<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------
namespace app\services\yibao;

use app\dao\car\CarOrderRefundDao;
use app\model\car\CarOrder;
use app\model\car\CarOrderRefund;
use app\services\BaseServices;
use app\services\pay\PayServices;
use app\services\user\UserMoneyServices;
use app\services\user\UserServices;
use crmeb\exceptions\AdminException;
use think\exception\ValidateException;
use think\facade\Db;

class YibaoServices extends BaseServices
{
    public function __construct(CarOrderRefundDao $dao)
    {
        $this->dao = $dao;
    }
    /*
       *易宝支付
       * 下单
       * 租金/押金都用这个方法
       * */
    public static function yibao_pay($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = []){
        $url = 'http://api.manager.eshouzl.com/cropland/placeOrder';//下单数据推送请求路径  正式环境
//        $url = 'http://172.17.3.78:8080/jeecg-boot/cropland/placeOrder';//下单数据推送请求路径  本地环境

        $data = [
            'appId'=>'mountain001',
            'msgId'=>'msg1534361236',
            'timestamp'=>date("Y-m-d H:i:s"),//date("Y-m-d H:i:s")
        ];
        if($attach=='deposit'){//押金支付
            p('待开发');exit;
        }else{//租金支付
            $data['otaContent'] = [
                'orderShareType'=>1,
                'storeNo'=>'KLCH1699414479192',
                'orderNo'=>$out_trade_no,
                'orderAmount'=>$total_fee,
                'goodsName'=>$body,
                'payWay'=>'MINI_PROGRAM',
                'channel'=>'WECHAT',
                'scene'=>'OFFLINE',
                'appId'=>'wx4c18f45fe5110fa1',
                'userId'=>$openid,
//              'userIp'=>request()->ip(),
                'userIp'=>'123.123.123.123',
                'expiredTime'=>'',
                'orderShareWay'=>4
            ];
            $json_data = json_encode($data['otaContent'],JSON_UNESCAPED_UNICODE);
            //进行签名
            $data['sign'] = getSign($json_data);
            $post_http = http_post_json($url,$data);
            $updatedata = [];
            if($post_http['code']==200){
                $updatedata = [
                    'storeNo'=>$post_http['result']['storeNo'],
                    'uniqueOrderNo'=>$post_http['result']['uniqueOrderNo'],
                    'bankOrderId'=>$post_http['result']['bankOrderId'],
                    'prePayTn'=>$post_http['result']['prePayTn'],
                ];
                $update = Db::name('car_order')->where('id',$out_trade_no)->update($updatedata);
            }
        }
        return $post_http;
    }

    //退单
    public static function yibao_refund($orderNo,$refundAmount,$description=''){
        //http://api.manager.eshouzl.com/cropland/refundOrder
        $url = 'http://172.17.3.78:8080/jeecg-boot/cropland/refundOrder';//退单数据推送请求路径
        $data = [
            'appId'=>'ezg1662434437611',
            'msgId'=>'msg1534361236',
            'timestamp'=>date("Y-m-d H:i:s"),//date("Y-m-d H:i:s")
        ];
        $data['otaContent'] = [
            'storeNo'=>'YJTY1666683255255',
            'orderNo'=>$orderNo,
            'refundAmount'=>$refundAmount,
            'description'=>$description,
        ];
        $json_data = json_encode($data['otaContent'],JSON_UNESCAPED_UNICODE);
        $data['sign'] = getSign($json_data);//进行签名
        $post_http = http_post_json($url,$data);
    }

}
